# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=lua5.4
_pkgname=lua
pkgver=5.4.6
_luaver=${pkgname#lua}
pkgrel=1
pkgdesc="Powerful light-weight programming language"
url="https://www.lua.org/"
arch="all"
license="MIT"
ldpath="/usr/lib/$pkgname"
depends_dev="$pkgname"
makedepends="libtool autoconf automake readline-dev"
provides="lua"
provider_priority=400
subpackages="$pkgname-dbg $pkgname-dev $pkgname-doc $pkgname-libs"
source="https://www.lua.org/ftp/$_pkgname-$pkgver.tar.gz
	lua-5.4-make.patch
	lua-5.4-module_paths.patch
	lua5.4.pc
	extern-c-for-cpp.patch
	"
builddir="$srcdir/$_pkgname-$pkgver"

# secfixes:
#   5.4.4-r4:
#     - CVE-2022-28805
#   5.3.5-r2:
#     - CVE-2019-6706

prepare() {
	default_prepare

	# Fill in data from pkgconfig file
	sed -e "s|@MAJOR_VER@|$_luaver|g" \
		-e "s|@MINOR_VER@|$pkgver|g" \
		-i "$srcdir"/lua5.4.pc

	# we use libtool
	cat > configure.ac <<-EOF
		top_buildir=.

		AC_INIT(src/luaconf.h)
		AC_PROG_LIBTOOL
		AC_OUTPUT()
	EOF
	libtoolize --force --install && aclocal && autoconf
}

build() {
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr

	cd src
	make V=${pkgver%.*} \
		CFLAGS="$CFLAGS -DLUA_USE_LINUX -DLUA_COMPAT_5_3 -DLUA_USE_READLINE" \
		SYSLDFLAGS="$LDFLAGS" \
		RPATH="/usr/lib" \
		LIB_LIBS="-lpthread -lm -ldl -lreadline" \
		alpine_all
}

check() {
	make test
}

package() {
	local i
	make V=${pkgver%.*} \
		INSTALL_TOP="$pkgdir"/usr \
		INSTALL_INC="$pkgdir"/usr/include/$pkgname \
		INSTALL_LIB="$pkgdir"/usr/lib/$pkgname \
		alpine_install

	for i in "$pkgdir"/usr/bin/* ; do
		mv "$i" "$i"$_luaver
	done

	for i in "$pkgdir/usr/lib/$pkgname"/*.so.*; do
		ln -s $pkgname/${i##*/} "$pkgdir"/usr/lib/${i##*/}
	done

	install -D -m 644 doc/lua.1 "$pkgdir"/usr/share/man/man1/lua$_luaver.1
	install -D -m 644 doc/luac.1 "$pkgdir"/usr/share/man/man1/luac$_luaver.1

	mkdir -p "$pkgdir"/usr/share/doc/$pkgname/
	install -m644 doc/*.html doc/*.css doc/logo.gif doc/osi-certified-72x60.png \
		"$pkgdir"/usr/share/doc/$pkgname/

	install -Dm644 "$srcdir"/lua5.4.pc -t "$pkgdir"/usr/lib/pkgconfig
	# compat name for "c++" lua (same thing with extern C
	ln -sv lua5.4.pc "$pkgdir"/usr/lib/pkgconfig/lua5.4-c++.pc
	# compat names since we are unique
	# other distros name it lua-5.4 or lua54. pick the former as a compat name..
	ln -sv lua5.4.pc "$pkgdir"/usr/lib/pkgconfig/lua-5.4-c++.pc
	ln -sv lua5.4.pc "$pkgdir"/usr/lib/pkgconfig/lua-5.4.pc
}

dev() {
	default_dev

	mkdir -p "$subpkgdir"/usr/$pkgname "$subpkgdir"/usr/lib/$pkgname
	ln -s ../include/$pkgname "$subpkgdir"/usr/$pkgname/include
	ln -s ../lib/$pkgname "$subpkgdir"/usr/$pkgname/lib
	mv "$pkgdir"/usr/lib/$pkgname/liblua.so "$subpkgdir"/usr/lib/$pkgname/
}

libs() {
	pkgdesc="Lua dynamic library runtime"
	replaces="lua"

	mkdir -p "$subpkgdir"/usr
	mv "$pkgdir"/usr/lib "$subpkgdir"/usr/
}

sha512sums="
d90c6903355ee1309cb0d92a8a024522ff049091a117ea21efb585b5de35776191cd67d17a65b18c2f9d374795b7c944f047576f0e3fe818d094b26f0e4845c5  lua-5.4.6.tar.gz
1bc6c623024c1738155b30ff9c0edcce0f336edc25aa20c3a1400c859421ea2015d75175cce8d515e055ac3e96028426b74812e04022af18a0ed4c4601556027  lua-5.4-make.patch
904a9b296d0bdb68630f8865fce86fc8f092120ee6ebfeb97f069b5aa49f203ddc63aeceee276ae8e006bbb73bf2811e7eceb6aae82817170d3acef3ad057c93  lua-5.4-module_paths.patch
34466bc244737a557ff2c87efbc62ab5c5424523f574d17b2f34aea40f534f2c45cdfa242097aadecd38f00d732b0c8e9daef1cd461728a084c72946edb9a48a  lua5.4.pc
e0932fedde440e76738725a6eca91bfdc94236fc1ead12731d303282c3cf721f09c601bae88dd9e6d7e77d89e5877ab18d720f146685a273318d33d48dedeba8  extern-c-for-cpp.patch
"
